<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.


include_once("ext_lib/simple_html_dom.php");


function execute_external_page($site_content){

    $html = str_get_html($site_content);

    foreach($html->find("a") as $element){
        if ($element->class == "ekkin" and $element->id == "external_page"){
            $href = $element->href;

            $params = "";
            foreach($_POST as $param => $arg){
                $arg2 = htmlentities($arg);
                $params .= "$param=$arg2&";
            }
            $l = $_GET["lang"];
            $params = rtrim($params, "&");

            $html2 = "<iframe id=\"external_page\" name=\"external_page\" src=\"$href?$params&lang=$l\" frameborder=\"0\" width=\"100%\" onload=\"resizeIframeToFitContent(this)\"></iframe>
            <script type=\"text/javascript\">
            function resizeIframeToFitContent(iframe) {
                //var external_page = document.getElementById('external_page');
                iframe.height = external_page.document.body.scrollHeight+50;
                iframe.width = external_page.document.body.scrollWidth;

                // write external stylesheel
                ";
            foreach ($html->find("link") as $e_css){
                if ($e_css->rel == "stylesheet" and $e_css->type == "text/css"){
                    $css_href = $e_css->href;
                    $html2 .= "var fileref=external_page.document.createElement(\"link\");
                fileref.setAttribute(\"rel\", \"stylesheet\");
                fileref.setAttribute(\"type\", \"text/css\");
                fileref.setAttribute(\"href\", \"$css_href\");
                external_page.document.getElementsByTagName(\"head\")[0].appendChild(fileref)
                ";
                }

            }

            $html2 .= "
            } // resizeIframeToFitContent
            </script>
            ";

            $element->outertext = $html2;
        }
    }
    return $html->save();

}

function execute_external_pagex($site_content){
    global $site_head_extra;

    $html = str_get_html($site_content);

    foreach($html->find("a") as $element){
        if ($element->class == "ekkin" and $element->id == "external_page"){

            // move scripts outside the div
            $html_external = file_get_html($element->href);
            foreach ($html_external->find("script") as $script){
                $html2 .= $script->outertext;
                $script->outertext = "";
            }
            $html_external->save();

            //$body = $html_external->find("body");
            //$head = $html_external->find("head");
            $href = $element->href;
            $params = "";
            foreach($_POST as $param => $arg){

                $params .= "$param=$arg&";
            }
            $html2 .= generate_external_page($href, rtrim($params, "&"));
            $element->outertext = $html2;

            //$site_head_extra .= $head[0]->innertext;
        }
    }

    return $html->save();
}

function generate_external_page($url, $params){
    $html = "<script type=\"text/javascript\">
function stateChanged()
{

}

function GetXmlHttpObject()
{
	var xmlHttp=null;

	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
	// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject(\"Msxml2.XMLHTTP\");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject(\"Microsoft.XMLHTTP\");
		}
	}
	return xmlHttp;
}

function getExternalPage(url, params) {
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert (\"Your browser does not support AJAX!\");
        return;
    }

    //xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open(\"POST\",url,false);
    xmlHttp.setRequestHeader(\"content-type\", \"application/x-www-form-urlencoded\");
    xmlHttp.send(\"$params\");
    if (xmlHttp.readyState==4){
        var content = xmlHttp.responseText;
        document.getElementById(\"external_page\").innerHTML = content;
    }
    else {
        document.getElementById(\"external_page\").innerHTML = \"failed to load external link\";
    }

}
    </script><div id=\"external_page\"></div>
    <script type=\"text/javascript\">
    document.getElementById(\"external_page\").innerHTML = \"failed to load external link\";

    getExternalPage('$url');</script>";


    return $html;





}

?>


